<template><div><h1 id="数组方法" tabindex="-1"><a class="header-anchor" href="#数组方法" aria-hidden="true">#</a> 数组方法</h1>
<p><img src="/images/Array.jpg" alt="" loading="lazy"></p>
<h2 id="_1-静态方法" tabindex="-1"><a class="header-anchor" href="#_1-静态方法" aria-hidden="true">#</a> 1.静态方法</h2>
<p>定义：可以通过Array构造器直接调用</p>
<h4 id="总结" tabindex="-1"><a class="header-anchor" href="#总结" aria-hidden="true">#</a> 总结</h4>
<table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>form</td>
<td>可以将一个伪数组转换成真数组</td>
</tr>
<tr>
<td>isArray</td>
<td>判断某个变量是否为一个数组，返回true或false</td>
</tr>
<tr>
<td>of</td>
<td>构造数组，传入参数为数组的每一项</td>
</tr>
</tbody>
</table>
<h4 id="_1-form" tabindex="-1"><a class="header-anchor" href="#_1-form" aria-hidden="true">#</a> 1.form</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">var</span> newArr<span class="token operator">=</span>Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span>   <span class="token comment">// [1, 2, 54, 89, 33]</span>
<span class="token punctuation">}</span>
<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">54</span><span class="token punctuation">,</span><span class="token number">89</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_2-isarray" tabindex="-1"><a class="header-anchor" href="#_2-isarray" aria-hidden="true">#</a> 2.isArray</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// false</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">...</span>arguments<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// true</span>
  <span class="token keyword">var</span> newArr<span class="token operator">=</span>Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span>  <span class="token comment">//[1,2,54,89,33]</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment">// true</span>
<span class="token punctuation">}</span>
<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">54</span><span class="token punctuation">,</span><span class="token number">89</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_3-of" tabindex="-1"><a class="header-anchor" href="#_3-of" aria-hidden="true">#</a> 3.of</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">var</span> newArr<span class="token operator">=</span>Array<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span>    <span class="token comment">// 1,4,23,4</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="_2-实例方法" tabindex="-1"><a class="header-anchor" href="#_2-实例方法" aria-hidden="true">#</a> 2.实例方法</h2>
<h4 id="总结-1" tabindex="-1"><a class="header-anchor" href="#总结-1" aria-hidden="true">#</a> 总结</h4>
<table>
<thead>
<tr>
<th>改变原数组方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>fill</td>
<td>用某个数据填充数组每一项(第二个参数表示从第几个下标开始填充)</td>
</tr>
<tr>
<td>push</td>
<td>在数组的尾部添加一项或多项数据（返回值为数组的新长度）</td>
</tr>
<tr>
<td>pop</td>
<td>删除数组的最后一项 （返回该项的值）</td>
</tr>
<tr>
<td>shift</td>
<td>删除数组的第一个元素（返回该项的值）</td>
</tr>
<tr>
<td>unshift</td>
<td>在数组开头添加一个或多个元素（返回值为数组的新长度）</td>
</tr>
<tr>
<td>splice</td>
<td>在任意位置给数组添加或删除任意个元素（3个参数，第一个表示位置下标,第二个表示要删除的个数）</td>
</tr>
<tr>
<td>reverse</td>
<td>将当前数组颠倒顺序（返回值为该数组）</td>
</tr>
<tr>
<td>sort</td>
<td>对数组进行排序，可以定义排序规则</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>不改变原数组方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>concat</td>
<td>组合连接数组，返回新数组</td>
</tr>
<tr>
<td>includes</td>
<td>数组中是否包含满足条件的元素，返回布尔</td>
</tr>
<tr>
<td>join</td>
<td>连接所有数组元素组成一个字符串</td>
</tr>
<tr>
<td>slice</td>
<td>抽取数组中的一段组成一个新数组</td>
</tr>
<tr>
<td>toString</td>
<td>返回一个有数组元素组成的字符串</td>
</tr>
<tr>
<td>indexOf</td>
<td>返回数组中第一个与指定值相当的元素的索引，找不到返回-1</td>
</tr>
<tr>
<td>lastIndexOf</td>
<td>返回数组中最后一个与指定值相当的元素的索引，找不到返回-1</td>
</tr>
<tr>
<td>forEach</td>
<td>遍历数组（第一个参数数组元素value，第二个为下表，第三个为数组本身）</td>
</tr>
<tr>
<td>every</td>
<td>是否数组的每一项都满足条件,返回布尔</td>
</tr>
<tr>
<td>some</td>
<td>是否数组有一项满足条件,返回布尔</td>
</tr>
<tr>
<td>filter</td>
<td>过滤。数组中满足条件的元素，返回到一个新数组中</td>
</tr>
<tr>
<td>find</td>
<td>查找第一个满足条件的元素，返回元素本身（没找到返回undefined）</td>
</tr>
<tr>
<td>findIndex</td>
<td>查找第一个满足条件的元素，返回下标（没找到返回-1）</td>
</tr>
<tr>
<td>map</td>
<td>映射。将数组中的每一项映射成一个新数组</td>
</tr>
<tr>
<td>reduce</td>
<td>统计。第一个参数为fun，第二个参数为默认值。（算法为每2项相加赋给第一项，再重复过程）</td>
</tr>
</tbody>
</table>
<h4 id="_1-fill" tabindex="-1"><a class="header-anchor" href="#_1-fill" aria-hidden="true">#</a> 1. fill</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">''</span><span class="token punctuation">,</span><span class="token string">''</span><span class="token punctuation">,</span><span class="token string">''</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_2-sort-排序" tabindex="-1"><a class="header-anchor" href="#_2-sort-排序" aria-hidden="true">#</a> 2. sort 排序</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code>eg1：按数字大小排序 从小到大排
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span>b</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> a<span class="token operator">-</span>b<span class="token punctuation">;</span>    
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code>eg2：随机排序
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span>b</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">0.5</span><span class="token punctuation">;</span>    
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_3-includes-是否包含" tabindex="-1"><a class="header-anchor" href="#_3-includes-是否包含" aria-hidden="true">#</a> 3.includes 是否包含</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
consile<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment">//第二个参数表示从哪个位置开始找</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_4-foreach-遍历" tabindex="-1"><a class="header-anchor" href="#_4-foreach-遍历" aria-hidden="true">#</a> 4.forEach 遍历</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>i<span class="token punctuation">,</span>arr</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span>i<span class="token punctuation">,</span>arr<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_5-every-判断" tabindex="-1"><a class="header-anchor" href="#_5-every-判断" aria-hidden="true">#</a> 5.every 判断</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token literal-property property">eg</span><span class="token operator">:</span>判断是否所有同学都及格
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">63</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> item<span class="token operator">>=</span><span class="token number">60</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>`
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_6-filter-过滤-查找所有" tabindex="-1"><a class="header-anchor" href="#_6-filter-过滤-查找所有" aria-hidden="true">#</a> 6.filter （过滤） 查找所有</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token literal-property property">eg</span><span class="token operator">:</span>拿到所有及格的分数
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">63</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> newArr<span class="token operator">=</span>arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> item<span class="token operator">>=</span><span class="token number">60</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_7-find-查找-第一个" tabindex="-1"><a class="header-anchor" href="#_7-find-查找-第一个" aria-hidden="true">#</a> 7.find 查找 （第一个）</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token literal-property property">eg</span><span class="token operator">:</span><span class="token function">拿到所有及格的分数</span><span class="token punctuation">(</span>找第一个<span class="token punctuation">)</span>
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'小米'</span><span class="token punctuation">,</span><span class="token literal-property property">score</span><span class="token operator">:</span><span class="token number">60</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'小超'</span><span class="token punctuation">,</span><span class="token literal-property property">score</span><span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'小红'</span><span class="token punctuation">,</span><span class="token literal-property property">score</span><span class="token operator">:</span><span class="token number">50</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'王'</span><span class="token punctuation">,</span><span class="token literal-property property">score</span><span class="token operator">:</span><span class="token number">65</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result<span class="token operator">=</span>arr<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> item<span class="token punctuation">.</span>score<span class="token operator">>=</span><span class="token number">60</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_8-map-映射" tabindex="-1"><a class="header-anchor" href="#_8-map-映射" aria-hidden="true">#</a> 8.map 映射</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token literal-property property">eg</span><span class="token operator">:</span>映射成学生分数 数组  <span class="token comment">//链式操作</span>
<span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">35</span><span class="token punctuation">,</span><span class="token number">96</span><span class="token punctuation">,</span><span class="token number">76</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result<span class="token operator">=</span>arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>i</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'学生'</span><span class="token operator">+</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token literal-property property">score</span><span class="token operator">:</span>item<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>i</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>           
  <span class="token keyword">return</span> item<span class="token punctuation">.</span>name
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_9-reduce-统计" tabindex="-1"><a class="header-anchor" href="#_9-reduce-统计" aria-hidden="true">#</a> 9.reduce 统计</h4>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">35</span><span class="token punctuation">,</span><span class="token number">96</span><span class="token punctuation">,</span><span class="token number">76</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> sum<span class="token operator">=</span>arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>result</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> item<span class="token operator">+</span>result<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span>                                       <span class="token comment">//数组为[],返回定义的0</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>sum<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="_3-实战练习" tabindex="-1"><a class="header-anchor" href="#_3-实战练习" aria-hidden="true">#</a> 3.实战练习</h2>
<h4 id="_1-作业" tabindex="-1"><a class="header-anchor" href="#_1-作业" aria-hidden="true">#</a> 1.作业</h4>
<p>var arr=[1,2,3,4,5,6,-1,-2,-3,-4,-5,-6];</p>
<p>去掉数组中的负数，然后对每一项平方，然后再对每一项翻倍，然后求和。</p>
<h2 id="附录" tabindex="-1"><a class="header-anchor" href="#附录" aria-hidden="true">#</a> 附录:</h2>
<p><a href="https://mp.weixin.qq.com/s/0c2rT5nbce13jkCoIPve4w" target="_blank" rel="noopener noreferrer">盘盘项目中你常用到的数组API<ExternalLinkIcon/></a></p>
</div></template>


